#!/bin/sh

start() {
	if ( lsmod | grep "nfivi_map" ) ; then
		echo "Error: modules exist, run 'control restart' instead."
		exit 1
	fi
	if [ ! -f nfivi_map.ko ] ; then
		echo "Error: modules are not compiled yet, make modules first."
		exit 1
	fi
	echo "Starting nfivi modules: "
	insmod nfivi_map.ko
	echo "  insmod nfivi_map.ko"
	insmod nfivi_xmit.ko
	echo "  insmod nfivi_xmit.ko"
	insmod nfivi_io.ko
	echo "  insmod nfivi_io.ko"
	insmod nfivi.ko
	echo "  insmod nfivi.ko"
	echo "Down!"
	if [ ! -e /dev/nfivi ] ; then
		echo "Make node at /dev/nfivi"
		mknod /dev/nfivi c 324 0
	fi
	return 0
}

stop() {
	if ( lsmod | grep "nfivi_map" ) ; then
		echo "Stopping nfivi modules: "
		rmmod nfivi
		echo "  rmmod nfivi"
		rmmod nfivi_io
		echo "  rmmod nfivi_io"
		rmmod nfivi_xmit
		echo "  rmmod nfivi_xmit"
		rmmod nfivi_map
		echo "  rmmod nfivi_map"
		echo "Down!"
		echo "Remove /dev/nfivi node"
		rm /dev/nfivi
	else
		echo "Error: modules do not exist."
	fi
	return 0
}

case "$1" in 
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		stop
		start
		;;
	*)
		echo "Usage: control {start|stop|restart|reload}"
		exit 1
esac

exit 0
